from enum import Enum
from typing import List, Union


class Enums(str, Enum):
    """
    周报chat回复消息类型
    """
    FEISHU_MEET_SUMMARY = 'feishu_meet_summary'
    FEISHU_SCHEDULE = 'feishu_schedule'
    FEISHU_TASKS = 'feishu_tasks'
    JIRA_LOGS = 'jira_logs'
    LAST_WEEKLY_REPORT = 'last_weekly_report'
    SUMMARIZE_TASKS = 'summarize_tasks'
    SUMMARIZE_TEAM = 'summarize_team'

    @property
    def title(self):
        mapping = {
            self.FEISHU_MEET_SUMMARY: '日程会议纪要',
            self.FEISHU_SCHEDULE: '飞书日程',
            self.FEISHU_TASKS: '飞书任务',
            self.JIRA_LOGS: 'JIRA',
            self.LAST_WEEKLY_REPORT: '工作周报',
            self.SUMMARIZE_TASKS: '任务维度总结',
            self.SUMMARIZE_TEAM: '团队维度总结'
        }
        return mapping[self]


class CollectionType(Enum):
    """集合类型枚举"""

    WITH_ATTENDEE = "with_attendee"  # 包含attendee_itcodes字段
    WITHOUT_ATTENDEE = "without_attendee"  # 不包含attendee_itcodes字段


class TimeDimension(str, Enum):
    """时间维度枚举"""

    THIS_WEEK = "this_week"
    LAST_WEEK = "last_week"
    THIS_MONTH = "this_month"
    LAST_MONTH = "last_month"
    THIS_QUARTER = "this_quarter"
    LAST_QUARTER = "last_quarter"
    THIS_YEAR = "this_year"

    @classmethod
    def from_chinese(cls, chinese_name: str):
        """从中文名称获取枚举值"""
        mapping = {
            "本周": cls.THIS_WEEK,
            "上周": cls.LAST_WEEK,
            "本月": cls.THIS_MONTH,
            "上月": cls.LAST_MONTH,
            "本季度": cls.THIS_QUARTER,
            "上季度": cls.LAST_QUARTER,
            "本年": cls.THIS_YEAR,
        }
        if chinese_name not in mapping:
            raise ValueError(f"不支持的时间维度: {chinese_name}")
        return mapping[chinese_name]


class QuarterMonthMapping:
    """季度到月份的映射"""

    # 季度到月份的映射表
    QUARTER_TO_MONTHS = {
        1: [1, 2, 3],  # 第一季度：1-3月
        2: [4, 5, 6],  # 第二季度：4-6月
        3: [7, 8, 9],  # 第三季度：7-9月
        4: [10, 11, 12],  # 第四季度：10-12月
    }

    @classmethod
    def get_months_from_quarter(cls, quarter: int) -> list[int]:
        """
        根据季度获取对应的月份列表

        Args:
            quarter: 季度（1-4）

        Returns:
            该季度包含的月份列表

        Raises:
            ValueError: 当季度参数不在1-4范围内时
        """
        if quarter not in cls.QUARTER_TO_MONTHS:
            raise ValueError(f"季度参数必须在1-4范围内，当前值: {quarter}")
        return cls.QUARTER_TO_MONTHS[quarter]


class FeishuDataType:
    """
    飞书数据查询类型配置
    """

    def __init__(self, class_type: Union[str, List[str]]):
        """
        初始化飞书数据类型

        Args:
            class_type: 可以是字符串或列表，如 ['日程', '会议纪要', '任务']
        """
        if isinstance(class_type, str):
            # 如果是字符串，尝试解析为列表
            try:
                import ast

                self.data_types = ast.literal_eval(class_type)
            except:
                # 如果解析失败，按逗号分割
                self.data_types = [item.strip() for item in class_type.split(",")]
        elif isinstance(class_type, list):
            self.data_types = class_type
        else:
            self.data_types = []

    @property
    def needs_meetings(self) -> bool:
        """是否需要查询会议数据"""
        return "会议纪要" in self.data_types

    @property
    def needs_schedules(self) -> bool:
        """是否需要查询日程数据"""
        return "日程" in self.data_types

    @property
    def needs_tasks(self) -> bool:
        """是否需要查询任务数据"""
        return "任务" in self.data_types


class CHAT_TYPE(Enum):
    """
    聊天类型
    """
    CHAT = ("chat", "智能回答")
    WEEKLY = ("weekly", "周报生成")
    WEEKLY_SUMMARY = ("weekly_summary", "周报总结")
    FEISHU_REVIEW = ("feishu_review", "飞书回顾")
    JIRA_REVIEW = ("jira_review", "Jira回顾")
    TASK_SUMMARY = ("task_summary", "任务维度总结")
    TEAM_SUMMARY = ("team_summary", "团队周报总结")
    PERSONAL_WEEKLY_SUMMARY = ("personal_summary", "个人周报总结")
    USER_FEEDBACK = ("user_feedback", "用户反馈")

    @property
    def name(self):
        return self.value[0]

    @property
    def desc(self):
        return self.value[1]

class REPORT_STYLE(Enum):
    """
    周报风格
    """
    DETAILED = "详细型"
    CONCISE = "简短型"

